package com.school.fuyuan.business.controller;

import com.school.fuyuan.business.constants.InfoConstants;
import com.school.fuyuan.business.model.po.InfoManage;
import com.school.fuyuan.business.model.vo.AddInfoVo;
import com.school.fuyuan.business.service.InfoManageService;
import com.school.fuyuan.common.annotation.Authenticate;
import com.school.fuyuan.common.message.RequestMsg;
import com.school.fuyuan.common.message.ResponseMsg;
import com.school.fuyuan.common.util.ParameterUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zrhong
 * @version 1.0
 * @date 2019/3/5 22:04
 * @description
 */
@RestController
public class InfoManageController {
    private final InfoManageService infoManageService;

    public InfoManageController(InfoManageService infoManageService) {
        this.infoManageService = infoManageService;
    }

    /**
     * 录入信息
     * @param addReq
     * @return
     */
    @PostMapping("/addInfo")
    public ResponseMsg addInfo(@RequestBody @Validated RequestMsg<AddInfoVo> addReq) {
        ResponseMsg responseMsg = new ResponseMsg();
        infoManageService.addInfo(addReq, responseMsg);
        return responseMsg;
    }


    /**
     * 获取信息列表
     *
     * @param requestMsg
     * @return
     */
    @PostMapping("getInfoManageList")
    @Authenticate(openAuth = true, authItems = {})
    public ResponseMsg<Map<String, Object>> getInfoManageList(@RequestBody @Valid RequestMsg requestMsg) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg();
        Map<String, Object> resultMap = infoManageService.getInfoManageList(requestMsg);
        responseMsg.setData(resultMap);
        return responseMsg;
    }

    /**
     * 修改信息
     * @param requestMsg
     * @return
     */
    @PostMapping("updateInfoManage")
    public ResponseMsg updateInfoManage(@RequestBody @Valid RequestMsg<InfoManage> requestMsg) {
        ResponseMsg<Map<String, List<InfoManage>>> responseMsg = new ResponseMsg();
        infoManageService.updateInfoManage(requestMsg);
        return responseMsg;
    }

    /**
     * 获取信息
     * @param requestMsg
     * @return
     */
    @PostMapping("getInfoManage")
    public ResponseMsg<InfoManage> getInfoManage(@RequestBody @Valid RequestMsg<Map<String, Object>> requestMsg) {
        ResponseMsg<InfoManage> responseMsg = new ResponseMsg();
        int id = Integer.valueOf(requestMsg.getData().get("id") + "");
        String flag = String.valueOf(requestMsg.getData().get("flag"));
//        String userId = requestMsg.getHead().getUserId();
        InfoManage infoManage = infoManageService.getInfoManage(id, flag);
        responseMsg.setData(infoManage);
        return responseMsg;
    }

    /**
     * 转跟进人
     * @param requestMsg
     * @return
     */
    @PostMapping("changFollow")
    public ResponseMsg changeFollow(@RequestBody @Valid RequestMsg<Map<String, Object>> requestMsg) {
        ResponseMsg responseMsg = new ResponseMsg();
        String userId = requestMsg.getHead().getUserId();
        String followUserId = (String) requestMsg.getData().get("followUserId");
        int id = Integer.valueOf(requestMsg.getData().get("id")+"") ;
        infoManageService.changeFollow(id, userId, followUserId);
        return responseMsg;
    }

    /**
     * 搜索信息
     * @param requestMsg
     * @return
     */
    @PostMapping("searchInfoManages")
    public ResponseMsg<Map<String, Object>> searchInfoManages(
            @RequestBody @Valid RequestMsg<Map<String, String>> requestMsg) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        Map<String, Object> resultMap = infoManageService.searchInfoManages(requestMsg);
        responseMsg.setData(resultMap);
        return responseMsg;
    }

    /**
     * 获取重复的信息
     * @param requestMsg
     * @return
     */
    @PostMapping("getRepeatInfos")
    public ResponseMsg<Map<String,Object>> getRepeatInfos(
            @RequestBody @Valid RequestMsg<Map<String, Object>> requestMsg) {
        ResponseMsg<Map<String,Object>> responseMsg = new ResponseMsg<>();
        Map<String,Object> resultMap = infoManageService.getRepeatInfos(requestMsg);
        responseMsg.setData(resultMap);
        return responseMsg;
    }

    /**
     * 转跟进人
     * @param requestMsg
     * @return
     */
    @PostMapping("deleteInfoMange")
    public ResponseMsg deleteInfoMange(@RequestBody @Valid RequestMsg<Map<String, List<Integer>>> requestMsg) {
        List<Integer> infoIds = requestMsg.getData().get("infoId");
        ResponseMsg responseMsg = new ResponseMsg();
        String userId = requestMsg.getHead().getUserId();
        ParameterUtils.assertValue("infoId", infoIds);
        infoManageService.deleteInfoManges(userId, infoIds);
        return responseMsg;
    }
}
